import {getUserAccount,getUserAgent,getShowUserAgent,getServerTime } from "@/api/user";
import { allAdminMultilingual,allProperty } from "@/api/game";
import { getGoods } from "@/api/activity";
import pubilc from '@/api/public.js'

const state = {
  languageDataArray: [],
  propDataArray: [],
  accountDataArray: [],
  goodDataArray: [],
  channelDataArray: [],
  channelShowDataArray: [],
  serverTime:''
};

const mutations = {
  UPDAYE_LANGUAGE_DATA: (state, languageDataArray = []) => {
    state.languageDataArray = languageDataArray;
  },

  UPDAYE_PROP_DATA: (state, propDataArray = []) => {
    state.propDataArray = propDataArray;
  },

  UPDAYE_ACCOUNT_DATA: (state, accountDataArray = []) => {
    state.accountDataArray = accountDataArray;
  },

  UPDAYE_GOOD_DATA: (state, goodDataArray = []) => {
    state.goodDataArray = goodDataArray;
  },

  UPDAYE_CHANNEL_DATA: (state, channelDataArray = []) => {
    state.channelDataArray = channelDataArray;
  },

  UPDAYE_SHOW_CHANNEL_DATA: (state, channelShowDataArray = []) => {
    state.channelShowDataArray = channelShowDataArray;
  },

  UPDAYE_TIME: (state, serverTime = '') => {
    state.serverTime = serverTime;
  },
};

const actions = {
  // 获取多语言
  async updateLanguageData({ commit },show) {
    let languageDataArray = [];
    if (state.languageDataArray.length && !show) {
      return;
    } else {
      let res = await allAdminMultilingual({game_id:-1});
      res.data?.forEach((s) => {
        const item = {
          value: s?.a_base_message_id,
          label: s?.c_base_zh_cn,
        };
        languageDataArray.push(item)
      });
    }
    commit("UPDAYE_LANGUAGE_DATA", languageDataArray || []);
  },

  // 获取道具
  async updatePropData({ commit },show) {
    let propDataArray = [];
    if (state.propDataArray.length && !show) {
      return;
    } else {
      let res = await allProperty({game_id:-1});
      res.data?.forEach((s) => {
        const item = {
          value: s?.a_base_item_id,
          label: s?.a_item_remark,
        };
        propDataArray.push(item)
      });
    }
    commit("UPDAYE_PROP_DATA", propDataArray || []);
  },

  // 获取所有商品信息
  async updateGoodData({ commit },show) {
    let goodDataArray = [];
    if (state.goodDataArray.length && !show) {
      return;
    } else {
      let res = await getGoods({page:1,size:500});
      res.data.list?.forEach((s) => {
        const item = {
          value: s?.id,
          label: s?.goods_name,
          goods_keys: s?.goods_keys,
          activity_type: s?.activity_type,
          goods_type: s?.goods_type,
        };
        goodDataArray.push(item)
      });
    }
    commit("UPDAYE_GOOD_DATA", goodDataArray || []);
  },

  // 获取所有用户
  async updateAccountData({ commit },show) {
    let accountDataArray = [];
    if (state.accountDataArray.length && !show) {
      return;
    } else {
      let res = await getUserAccount({});
      res.data?.forEach((s) => {
        const item = {
          value: s?.id,
          label: s?.nick_name,
        };
        accountDataArray.push(item)
      });
    }
    commit("UPDAYE_ACCOUNT_DATA", accountDataArray || []);
  },

  // 获取所有渠道信息
  async updateChannelData({ commit },show) {
    let channelDataArray = [];
    if (state.channelDataArray.length && !show) {
      return;
    } else {
      let res = await getUserAgent();
      res.data?.forEach((s) => {
        const item = {
          value: s?.id,
          label: s?.nick_name,
        };
        channelDataArray.push(item)
      });
    }
    commit("UPDAYE_CHANNEL_DATA", channelDataArray || []);
  },

  // 获取所有可展示渠道信息
  async updateShowChannelData({ commit },show) {
    let channelShowDataArray = [];
    if (state.channelShowDataArray.length && !show) {
      return;
    } else {
      let res = await getShowUserAgent({});
      localStorage.setItem('ShowAccountTime',new Date().getTime())
      res.data?.forEach((s) => {
        const item = {
          value: s?.id,
          label: s?.nick_name,
        };
        channelShowDataArray.push(item)
      });
    }
    commit("UPDAYE_SHOW_CHANNEL_DATA", channelShowDataArray || []);
  },

  // 获取服务器时间
  async updateTimeData({ commit },show) {
    let serverTime = '';
    if (state.serverTime && !show) {
      return;
    } else {
      let res = await getServerTime();
      serverTime=res.data.serverTime
      pubilc.getServerTime(serverTime) //服务器时间
      commit("UPDAYE_TIME", serverTime || new Date());
    }
  },
};

const getters = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
